﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.Text.RegularExpressions;

using System.Net;
using System.IO;

namespace QuickJob
{
    public class htmlOper
    {


        /// <summary>
        /// 图片列表
        /// </summary>
        public List<string>lpictures = new List<string>();
        /// <summary>
        /// 价格列表
        /// </summary>
        public List<float> lprices = new List<float>();


        public string htmlurl = "";
        /// <summary>
        /// 网页内容
        /// </summary>
        public string htmlContent;

        public event Ulitily.OnDownLoadProgress onDownLoadProgress;

        public static string testUrl = "http://gz.17zwd.com/item.htm?GID=6789958&spm=s4xdQ.42.50702.15810.6789958.5114";
        /// <summary>  
        /// 获取网页的HTML码  
        /// </summary>  
        /// <param name="url">链接地址</param>  
        /// <param name="encoding">编码类型</param>  
        /// <returns></returns>  
        public static string GetHtmlStr(string url, string encoding)
        {
            string htmlStr = "";
            if (!String.IsNullOrEmpty(url))
            {
                WebRequest request = WebRequest.Create(url);            //实例化WebRequest对象  
                WebResponse response = request.GetResponse();           //创建WebResponse对象  
                Stream datastream = response.GetResponseStream();       //创建流对象  
                Encoding ec = Encoding.Default;
                if (encoding == "UTF8")
                {
                    ec = Encoding.UTF8;
                }
                else if (encoding == "Default")
                {
                    ec = Encoding.Default;
                }
                StreamReader reader = new StreamReader(datastream, ec);
                htmlStr = reader.ReadToEnd();                           //读取数据  
                reader.Close();
                datastream.Close();
                response.Close();
            }
            return htmlStr;
        }



        /// <summary>  
        /// 下载网站图片  
        /// </summary>  
        /// <param name="picUrl"></param>  
        /// <returns>返回图片名称，null，为不成功</returns>  
        public static string SaveAsWebImg(string picUrl,string directory)
        {
            string result = null;
            string fullname = null;
            string subname = "jpg";
            try
            {
                if (!String.IsNullOrEmpty(picUrl))
                {
                    DateTime nowTime = DateTime.Now;
                    String ms = String.Format("{0:D4}", nowTime.Millisecond);
                    if (picUrl.Contains("jpg")) subname = ".jpg";
                    else if (picUrl.Contains("jpeg")) subname = ".jpeg";
                    else if (picUrl.Contains("png")) subname = ".png";
                    
                    string fileName = nowTime.ToString("yyMMddhhmmss_") + ms + subname;
                    fullname = System.IO.Path.Combine(directory, fileName);
                    WebClient webClient = new WebClient();
                    webClient.DownloadFile(picUrl, fullname);
                    result = fullname;
                }
            }
            catch { }
            return result;
        }





        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="url"></param>
        public void HtmlLoadData(string url)
        {
            htmlurl = url;
            this.htmlContent = GetHtmlStr(url, "UTF8");

        }

        /// <summary>
        /// 读取所有的 图片连接
        /// </summary>
        /// <returns>返回图片连接</returns>
        public List<string> HtmlGetAllPictures()
        {
            List<string> lpic = new List<string>();
            string picPatten = @"<[img|href][^>]*src\s*=\s*('|" + "\")?([^'" + "\">]*)" + @"\1([^>])*>";
            MatchCollection mc = Regex.Matches(htmlContent, picPatten);
            if (mc.Count < 1) return null;

            //对 一 条条 <>  格式 建 树
            for (int i = 0; i < mc.Count; i++)
            {


               // string mstring = @"((http|ftp|https)://)(([^" + "\"]\\S)*)|((//img.)(([^" + "\"]\\S)*))";
                //string mstring = @"((http|ftp|https)://)((\S)*[$"+"\"$'])|((//img.)((\\S)*[$"+"\\" + "\"$']))";
                string mstring = @"(((http|ftp|https)://)(\S)*(.(jpg|jpeg|png))) |((//img.)(\S)*(.(jpg|jpeg|png)))";
                    Match spic = Regex.Match(mc[i].Value, mstring);
                    if (spic.Success)
                        lpic.Add(spic.Value);
            }
            this.lpictures = lpic;
            return lpic;
        }

        Size HtmlGetPictureSize(string url)
        {
            Size s = new Size(0, 0);
            try
            {
                WebRequest webreq = WebRequest.Create(url);
                //红色部分为文件URL地址，这里是一张图片
                WebResponse webres = webreq.GetResponse();
                Stream stream = webres.GetResponseStream();
                System.Drawing.Image image;
                image = System.Drawing.Image.FromStream(stream);
                stream.Close();
                s.Width = image.Width;
                s.Height = image.Height;
                image.Dispose();

            }
            catch { }

            return s;
        }

        /// <summary>
        /// 下载所有大图
        /// </summary>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <param name="wmin">最小值</param>
        /// <param name="hmin">最小值</param>
        /// <param name="dir"></param>
        /// <returns>大图名称 </returns>
        public List<string> HtmlDownloadBigPictures(int w, int h,string dir,int wmin,int hmin)
        {
            string tmp = null;
            List<string> lpic = lpictures;
            string name = null;
            int Offset = 0;
            List<string> names = new List<string>();
            try
            {
                if (lpic != null && lpic.Count > 0)
                {
                    foreach (string pstring in lpic)
                    {

                        //////////////////////////////////////////////////////////////////////////
                        //处理进度
                        Offset++;
                        if (onDownLoadProgress != null)
                        {
                            Ulitily.DownLoadProgressEventArg args = new Ulitily.DownLoadProgressEventArg();
                            args.Count = lpic.Count;
                            args.now = Offset;
                            onDownLoadProgress(this, args);
                        }

                        //////////////////////////////////////////////////////////////////////////
                        //转换地址
                        string url = "";
                        tmp = pstring;
                        if (tmp.StartsWith("//"))
                            url = "http:" + pstring;
                        else
                            url = pstring;
                        //////////////////////////////////////////////////////////////////////////
                        //读取图片大小
                        Size s = HtmlGetPictureSize(url);
                        if (s.Width < w &&  s.Height < h) continue;
                        if (s.Width < wmin || s.Height < hmin) continue;

                        //////////////////////////////////////////////////////////////////////////
                        //下载图片
                        name = SaveAsWebImg(url, dir);
                        if (name != null)
                                names.Add(name);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("无法获取图片:" + tmp);
            }

            return names;
        }


        /// <summary>
        /// 取得所有价格
        /// </summary>
        /// <returns></returns>
        public List<float> HtmlGetAllPrices()
        {
            MatchCollection mc = Regex.Matches(htmlContent, "\"price2\"" +":(\\d|[.])*");
            if (mc.Count > 0)
            {
                List<float> lp = new List<float>();
                for (int i = 0; i < mc.Count; i++)
                {
                    Match mm = Regex.Match(mc[i].Value, @"(\d)([.]|\d)*(\d)");
                    if (mm.Success)
                    {
                        try
                        {
                            lp.Add(float.Parse(mm.Value));
                        }
                        catch { }

                    }
                }
                lprices = lp;
                return lp;

            }

            return null;
        }

        public string HtmlGetPriceString()
        {
            HtmlGetAllPrices();
            if (lprices.Count == 0) return "";
            float mix = lprices.Min();
            float max = lprices.Max();
            string s = "";

            if (mix < max) {
                max *= 100;
                s = string.Format("{0:D2}-{1:D4}", (UInt32)mix, (UInt32)max);
            }

            else s = string.Format("{0:D4}",(UInt32)(max * 100));


            return s;
        }




        /// <summary>
        /// 读取商城 信息
        /// </summary>
        /// <returns></returns>
        public string HtmlGetShangCheng()
        {
            string ShangCheng = "";
            try
            {
               
                if (htmlContent == null) return ShangCheng;
                MatchCollection mc = Regex.Matches(htmlContent, @"(<div ([^>])*>)([^<])*(</div>)");
                if (mc.Count > 0)
                {
                    for (int i = 0; i < mc.Count; i++)
                    {
                        if (Regex.Match(mc[i].Value, @"商城").Success)
                        {
                            string sh = mc[i + 1].Value.ToString();
                            sh = sh.Split('>')[1].Split('<')[0];
                            ShangCheng = sh;
                            break;
                        }
                    }
                }
            }
            catch { }

            return ShangCheng;

        }


        /// <summary>
        /// 读取货号
        /// </summary>
        /// <returns></returns>
        public string HtmlGetHuoHao()
        {
            string huohao = null;
            try
            {
                if (htmlContent == null) return huohao;
                Match m = Regex.Match(htmlContent, @"货号：([\d]|[A-Za-z])*");
                if (m.Success)
                {
                    huohao = Regex.Match(m.Value, @"([A-Za-z0-9][0-9]+)").Value;
                }
                else
                {

                    //没有货号 ，从 标题中，找到货号
                    Regex re = new Regex(@"\d+");
                    MatchCollection mc = re.Matches(this.HtmlGetTitle());

                    if (mc.Count > 0)
                    {
                        for (int i = 0; i < mc.Count; i++)
                        {
                            string str = mc[i].Value;
                            if (str != "2016") { return str; }
                        }
                    }

                }

            }
            catch { }

            return huohao;
              
        }


        public string HtmlGetTitle()
        { 
            Match m = Regex.Match(htmlContent,"<div class=\"goods-page-show-title\">([\\S|\\s])*");
            if (m.Success) {
                try
                {
                    string s = m.Value.Split('>')[1].Split('<')[0].ToString();
                    return s;
                }
                catch { }
            }

            return "";
        }

        /// <summary>
        /// 保存网页为文件　html格式
        /// </summary>
        /// <returns></returns>
        public string HtmlToDocumentation(string url,string header)
        {
            if (url == null) url = htmlurl;
            if (header == null || header == "") header = "宝贝网址";
            string enter = "\r\n";
            String html = "<html>" + enter + "<head>" + enter +
                 "<title>宝贝网址</title>" + enter +
               "</head>" + enter +
               "<body>" + enter +
               "<font size=\"10\">" + enter +
               "<a href=\"" + url  + "\">" + header + "</a>" + enter +
               "</font>" + enter +

               "<script language=\"javascript\" type=\"text/javascript\"> " + enter +
               // 以下方式直接跳转
                "window.location.href=\'" + url + "\';" + enter + 


               "</script>" + enter +
               "</body>" + enter +
               "</html>" + enter;
            return html;
        }


        /// <summary>
        /// 保存网页文件到路径
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public string HtmlSaveDocumentation(string dir)
        {
            FileStream fs = File.Create(dir + "\\" + "html.html");

            try
            {
                ShangPinDescription description = GetDescription();
                string myUrl = this.htmlurl;
                string mytitle = description.title;
                string myjiage = description.jiage;
                string myshangche = description.shangcheng;
                string myhuohao = description.huohao;
                string mypinming = description.pinming;
                String enter = "\r\n";
                
                String html = "<html>" + enter + "<head>" + enter +
                     "<title>" + mytitle + "</title>" + enter +
                   "</head>" + enter +
                   "<body>" + enter +
                   "<font size=\"10\">" + enter +
                   "<a href=\"" + myUrl + "\">" + mytitle + "</a>" + enter +
                   "</font>" + enter +

                   "<script language=\"javascript\" type=\"text/javascript\"> " + enter +
                    // 以下方式直接跳转
                    "window.location.href=\'" + myUrl + "\';" + enter +
                   "</script>" + enter +

                   "<br><hr>"+enter+"<p>"+
                   // 保存其它信息+
                   "[商城=" + myshangche + "]"+ enter +
                   "[价格=" + myjiage + "]" + enter +
                   "[货号=" + myhuohao + "]" + enter +
                   "[品名=" + mypinming + "]" + enter +

                   "</p><hr>"+enter + 
                   "</body>" + enter +
                   "</html>" + enter;


                byte[] data = System.Text.Encoding.Default.GetBytes(html + "\t" + "\r\n");
                fs.Write(data, 0, data.Length);
                fs.Close();

                return html;



            }
            catch { fs.Close(); return null; }

        }

        /// <summary>
        /// 读取记录中的品名
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static string HtmlGetPinMingFromDocumentation(string dir)
        {
            try
            {
                string slines = "";

                StreamReader rd;
                String fn = System.IO.Path.Combine(dir, "html.html");
                try
                {

                    rd = new StreamReader(fn, Encoding.Default);
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("html.html无法打开:" + fn);
                    return null;
                }


                string line;

                ///读入文件
                for (line = rd.ReadLine(); line != null; line = rd.ReadLine())
                { slines += line; }
                rd.Close();

                ///搜索配置
                Regex re = new Regex(@"[^\[]*[$\]]");
                MatchCollection mc = re.Matches(slines);

                if (mc.Count > 0)
                {
                    for (int i = 0; i < mc.Count; i++)
                    {
                        string V = mc[i].Value.Replace("]", "");
                        if (V.Contains("品名"))
                        {
                            string pin = V.Split('=')[1];
                            return pin;
                        }
                    }
                }

            }
            catch { }
            return null;

        }
        
        
        /// <summary>
        /// 读取品名
        /// </summary>
        /// <returns></returns>
        public string HtmlGetPinMing()
        {
            try {
                Regex re = new Regex("<meta name=\"keywords\"([^/])*/>");
                Match m = re.Match(this.htmlContent);
                if (m.Success)
                {
                    string V = m.Value;

                    re = new Regex(@"\s[\u4e00-\u9fa5]*\s-");
                    m = re.Match(V);
                    if (m.Success)
                    {
                        V = m.Value.Replace("-", "").Replace(" ", "");
                        return V;
                    }
                    else
                    {

                        //按照 顺序  取品名
                        




                    }

                }
            }
            catch { }

            return null;
        }

        //////////////////////////////////////////////////////////////////////////
        public class ShangPinDescription{
            public string title;        //标题
            public string pinming;      //品名
            public string huohao;       //货号
            public string shangcheng;   //商城
            public string jiage;        //价格
        }
        //读取商品描述 信息
        public ShangPinDescription GetDescription()
        {
            ShangPinDescription description = new ShangPinDescription();
            
            //标题
            description.title = HtmlGetTitle();
            description.huohao = HtmlGetHuoHao();
            description.jiage = HtmlGetPriceString();
            description.shangcheng = HtmlGetShangCheng();

            //品名读取
            description.pinming = HtmlGetPinMing();

            return description;
        }
    }
}
